package com.wkck.controller.admin;

import com.wkck.VO.AdminLoginVo;
import com.wkck.constant.JwtClaimsConstant;
import com.wkck.context.BaseContext;
import com.wkck.domain.Admin;
import com.wkck.dto.PasswordDto;
import com.wkck.properties.JwtProperties;
import com.wkck.result.Result;
import com.wkck.service.AdminService;
import com.wkck.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@CrossOrigin
@Api(tags = "管理员业务接口")
@Slf4j
public class AdminBgController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 管理端登录
     * @param adminDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "登录")
    public Result<AdminLoginVo> adminLogin(@RequestBody Admin adminDTO) {
        log.info("需要进行登录的管理员信息: {}", adminDTO);
        Admin admin = adminService.adminLogin(adminDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, admin.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        AdminLoginVo adminLoginVo = AdminLoginVo.builder()
                .id(admin.getId())
                .admin(admin.getAdmin())
                .token(token)
                .email(admin.getEmail())
                .avatar(admin.getAvatar())
                .role(admin.getRoles())
                .introduction(admin.getIntroduction())
                .phone(admin.getPhone())
                .build();

        return Result.success(adminLoginVo);
    }

    /**
     * 管理员账户登出
     * @return
     */
    @GetMapping("/logout")
    @ApiOperation(value = "登出")
    public Result adminLogout() {
        log.info("退出登陆成功: {}", BaseContext.getCurrentId());
        return Result.success();
    }

    /**
     * 校验管理员账户密码
     * @param passwordDto
     * @return
     */
    @PostMapping("/validPassword")
    @ApiOperation(value = "校验管理员账户密码")
    private Result<Boolean> validPassword(@RequestBody PasswordDto passwordDto) {
        log.info("需校验的密码: {}", passwordDto);
        return Result.success(adminService.validPassword(passwordDto));
    }


    /***
     * 修改管理员账户信息
     * @return
     */
    @PutMapping("/edit")
    @ApiOperation(value = "修改管理员账户信息")
    public Result<AdminLoginVo> edit(@RequestBody Admin adminDto) {
        log.info("需要修改的信息: {}", adminDto);
        return Result.success(adminService.edit(adminDto));
    }

    /**
     * 查询所有管理员
     * @return
     */
    @GetMapping("/queryAdminList")
    @ApiOperation(value = "查询全部管理员")
    public Result<List<Admin>> queryAdmin() {
        return Result.success(adminService.list());
    }
}
